#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

class Solution {
public:
    int longestOnes(vector<int>& nums, int k)
    {
        int ret = -1;
        int left = 0, right = 0;
        while (right < nums.size())
        {
            if (nums[right] == 0 && k == 0)
            {
                ret = max(ret, right - left);
                while (nums[left] != 0)
                    left++;
                left++;
                right++;
                continue;
            }
            else if (nums[right] == 0 && k > 0)
            {
                k--;
                right++;
                continue;
            }
            right++;
        }
        ret = max(ret, right - left);
        return ret;
    }
};